package com.litepaldemo;

import android.content.ContentValues;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.litepaldemo.db.HeroBean;
import com.litepaldemo.db.MingWen;

import org.litepal.crud.DataSupport;
import org.litepal.tablemanager.Connector;

import java.util.Date;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class MainActivity extends AppCompatActivity {

    @Bind(R.id.btn_name)
    EditText mBtnName;
    @Bind(R.id.btn_property)
    EditText mBtnProperty;
    @Bind(R.id.btn_attack)
    EditText mBtnAttack;
    @Bind(R.id.btn_defend)
    EditText mBtnDefend;
    @Bind(R.id.insert)
    Button mInsert;
    @Bind(R.id.et_tablename)
    EditText mEtTablename;
    @Bind(R.id.et_row_id)
    EditText mEtRowId;
    @Bind(R.id.et_column_name)
    EditText mEtColumnName;
    @Bind(R.id.et_column_value)
    EditText mEtColumnValue;
    @Bind(R.id.et_mw_name)
    EditText mEtMwName;
    @Bind(R.id.et_mw_property)
    EditText mEtMwProperty;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);

        SQLiteDatabase db = Connector.getDatabase();
    }

    private void insertHeroBeanData() {
        String name = mBtnName.getText().toString().trim();
        String property = mBtnProperty.getText().toString().trim();
        String attack = mBtnAttack.getText().toString().trim();
        String defend = mBtnDefend.getText().toString().trim();

        HeroBean heroBean = new HeroBean();
        heroBean.setName(name);
        heroBean.setProperty(property);
        heroBean.setAttack(Float.parseFloat(attack));
        heroBean.setDefend(Float.parseFloat(defend));
        heroBean.setPublishDate(new Date());
        boolean save = heroBean.save();

        if (save) {
            Toast.makeText(this, "save successful", Toast.LENGTH_SHORT).show();
        } else {
            Toast.makeText(this, "save failed", Toast.LENGTH_SHORT).show();
        }


    }



    private void upData() {
        String columnName = mEtColumnName.getText().toString().toString();
        String columnValue = mEtColumnValue.getText().toString().trim();
        String tableName = mEtTablename.getText().toString().trim();
        String rowId = mEtRowId.getText().toString().trim();

        ContentValues contentValues = new ContentValues();
        contentValues.put(columnName, columnValue);
        switch (tableName) {
            case "herobean":
                int i = DataSupport.update(HeroBean.class, contentValues, Long.parseLong(rowId));
                Toast.makeText(this, "updata 第 " + i + " 行数据！", Toast.LENGTH_SHORT).show();
                break;
            case "mingwen":
                int j = DataSupport.update(MingWen.class, contentValues, Long.parseLong(rowId));
                Toast.makeText(this, "updata 第 " + j + " 行数据！", Toast.LENGTH_SHORT).show();
                break;
            default:
        }
    }

    private void insertMingWenData() {
        String name = mEtMwName.getText().toString().trim();
        String property = mEtMwProperty.getText().toString().trim();

        MingWen mingWen = new MingWen();
        mingWen.setName(name);
        mingWen.setProperty(property);
        boolean save = mingWen.save();

        if (save) {
            Toast.makeText(this, "save successful", Toast.LENGTH_SHORT).show();
        } else {
            Toast.makeText(this, "save failed", Toast.LENGTH_SHORT).show();
        }

    }


    @OnClick({R.id.insert, R.id.updata, R.id.btn_insert_mw})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.insert:
                insertHeroBeanData();
                break;
            case R.id.btn_insert_mw:
                insertMingWenData();
                break;
            case R.id.updata:
                upData(); // 根据 行id修改
                break;

        }
    }



}
